<template>
  <a-modal
    :title="title"
    :width="780"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="{span: 6}">
          <a-select v-decorator="['status', { rules: [{ required: true, message: '请选择预约类型' }] }]">
            <a-select-option v-for="(item) in statusList" :key="item.value" :value="item.value">{{
              item.name
            }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import moment from 'moment'
import pick from 'lodash.pick'
import * as Api from '@/api/appointment/index'
import * as UserApi from '@/api/appointment/user'
import * as CarTypeApi from '@/api/appointment/carType'
import AppointmentTypeEnum from '@/common/enum/appointment/AppointmentType'
import FormTypeEnum from '@/common/enum/appointment/FormType'
import FormStatusEnum from '@/common/enum/appointment/FormStatus'

export default {
  props: {
    // 分类列表
    categoryList: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      // 对话框标题
      title: '编辑文章',
      // 标签布局属性
      labelCol: {
        span: 7,
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13,
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录ID
      appointmentId: null,
      // 当前记录详情
      record: {},
      // 车辆种类列表
      carTypeList: [],
      // 用户列表
      userList: [],
    }
  },
  computed: {
    formTypeList() {
      return FormTypeEnum.getData()
    },

    appointmentTypeList() {
      return AppointmentTypeEnum.getData()
    },

    statusList() {
      return FormStatusEnum.getData()
    }
  },
  methods: {
    /**
     * 显示对话框
     */
    edit(appointmentId) {
      // 显示窗口
      this.visible = true
      // 当前记录ID
      this.appointmentId = appointmentId
      // 获取当前详情记录
      this.getDetail()
      // 获取用户列表
      this.getUserList()
      // 获取车辆种类列表
      this.getCarTypeList()
    },

    // 获取当前详情记录
    getDetail() {
      this.confirmLoading = true
      Api.detail({ appointmentId: this.appointmentId })
        .then((result) => {
          // 当前记录
          this.record = result.data.form
          // 设置默认值
          this.setFieldsValue()
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },

    // 获取用户列表
    getUserList() {
      return new Promise((resolve, reject) => {
        UserApi.all()
          .then((res) => {
            this.userList = res.data.list
          })
          .finally(() => {
            resolve()
          })
      })
    },

    // 获取车辆种类列表
    getCarTypeList() {
      return new Promise((resolve, reject) => {
        CarTypeApi.list()
          .then((res) => {
            console.log(res)
            this.carTypeList = res.data.list
          })
          .finally(() => {
            resolve()
          })
      })
    },

    onReloadCarTypeList() {
      this.isLoading = true
      this.getCarTypeList().then(() => {
        this.isLoading = false
      })
    },

    // 刷新用户列表
    onReloadUserList() {
      this.isLoading = true
      this.getUserList().then(() => {
        this.isLoading = false
      })
    },

    /**
     * 设置默认值
     */
    setFieldsValue() {
      const {
        form: { setFieldsValue },
      } = this
      this.$nextTick(() => {
        const data = pick(this.record, [
          'form_type',
          'user_id',
          'phone',
          'address',
          'brand',
          'mileage',
          'car_type_id',
          'appointment_type',
          'time',
          'status'
        ])
        data.time = moment(this.record.time)
        setFieldsValue(data)
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const {
        form: { validateFields },
      } = this
      validateFields((errors, values) => {

        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
      this.record = {}
      this.form.resetFields()
    },

    /**
     * 提交到后端api
     */
    onFormSubmit(values) {
      this.confirmLoading = true
      values.time = moment(values.time).format('YYYY-MM-DD HH:mm:ss')

      let d = { appointmentId: this.appointmentId, form: values }
      d.form.user_id = this.record.user_id

      Api.edit(d)
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },
  },
}
</script>
<style scoped>
.ant-input-number {
  width: 200px !important;
}
</style>